﻿using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DarknetJsonReceiver
{
    class Program
    {
        private static readonly HttpClient client = new HttpClient();

        static async Task Main(string[] args)
        {
            await ProcessRepositories();
            /*IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8070);
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(ipe);
            Byte[] bytesReceived = new Byte[256];
            int bytes = 0;
            string page = "";
            do {
                bytes = socket.Receive(bytesReceived);
                page = Encoding.ASCII.GetString(bytesReceived, 0, bytes);
                Console.WriteLine(page);
               // Thread.Sleep(10000);
            }
            while (bytes > 0);*/
            /*while (true)
            {
                byte[] buffer = new byte[1024 * 1024 * 2];
                socket.Receive(buffer);
                string ResponseData = Encoding.ASCII.GetString(buffer, 0, buffer.Length);
                Console.Write(ResponseData);
            }*/
            /*TcpClient tcpClient = new TcpClient("localhost",8070);
            NetworkStream datastream = tcpClient.GetStream();
            while (true)
            {
                byte[] data = new byte[256];
                int bytes = 256;
                while (bytes == 256)
                {
                    bytes = datastream.Read(data, 0, data.Length);
                    string ResponseData = Encoding.ASCII.GetString(data, 0, data.Length);
                    Console.Write(ResponseData);
                }
                //datastream.Dispose();
                Thread.Sleep(100);
            }
            tcpClient.Close();
            Console.WriteLine("Hello World!");
            
            Console.ReadLine();*/
            Console.WriteLine("Hello World!");
        }

        private static async Task ProcessRepositories()
        {
            //client.DefaultRequestHeaders.Accept.Clear();
            //client.DefaultRequestHeaders.Accept.Add(
              //  new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
            //client.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");

            var streamTask = client.GetStreamAsync("http://localhost:8070/");

            var stream = await streamTask;
            while (true)
            {
                Byte[] bytesReceived = new Byte[256];
                int bytes = await stream.ReadAsync(bytesReceived, 0, bytesReceived.Length);
                string ResponseData = Encoding.ASCII.GetString(bytesReceived, 0, bytesReceived.Length);
                Console.Write(ResponseData);
            }
        }
    }
}
